﻿Imports System.IO
Public Class frmEditReceiveItem
    Dim Strsql As String
    Dim _mysql As New ClsSQLhelper
    Dim On_load As Boolean = True
    Public Primary_Key As Integer = -1
#Region "Function"
    Sub Copy_Image2Server()
        Dim StrFilename As String = ""
        Try

            StrFilename = My.Settings.ImgSharePart & "\" & Now.Year.ToString & Now.Month.ToString & Now.Day.ToString & Now.Hour & Now.Minute & Now.Second & ".jpg"

            File.Copy(Me.TxtImagepart.Text, StrFilename, True)
            Dim bm As New Bitmap(Me.TxtImagepart.Text)
            PictureEdit1.Image = bm
            Me.TxtImagepart.Text = StrFilename
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Copy_Image2Server"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = "StrFilename = " & StrFilename
                .ShowDialog()
            End With
        End Try

    End Sub
    Sub Load_Data()
        Try

            Strsql = "Select doc_head_id,doc_rowid,productname,serialno,band,model,priceperunit,vat,priceincludevat,"
            Strsql = Strsql & " drawqty,receiveqty,totalpriceprice,staff,remarks,purchase_date,mdf_date,warantry,nonvat,nonmfd,nonprice,nonbuydate,imagepart,item_type_id from "
            Strsql = Strsql & " pr_detail where doc_detail_id = " & Primary_Key
            Dim DT_Doc_Detail_Id As New DataTable
            DT_Doc_Detail_Id = _mysql.GetMYSQLDataTable(Strsql, "pr_detail")
            TxtName.Text = DT_Doc_Detail_Id.Rows(0).Item("productname").ToString
            TxtBand.Text = DT_Doc_Detail_Id.Rows(0).Item("band").ToString
            TxtModel.Text = DT_Doc_Detail_Id.Rows(0).Item("model").ToString
            TxtPrice.Value = DT_Doc_Detail_Id.Rows(0).Item("priceperunit").ToString
            TxtPriceIncludeVat.Value = DT_Doc_Detail_Id.Rows(0).Item("priceincludevat").ToString
            TxtQty.Value = DT_Doc_Detail_Id.Rows(0).Item("drawqty").ToString
            TxtReceiveQty.Value = DT_Doc_Detail_Id.Rows(0).Item("receiveqty").ToString
            TxtRemarks.Text = DT_Doc_Detail_Id.Rows(0).Item("remarks").ToString
            TxtSerialno.Text = DT_Doc_Detail_Id.Rows(0).Item("serialno").ToString
            TxtStaff.Text = DT_Doc_Detail_Id.Rows(0).Item("staff").ToString
            TxtTotalprice.Value = DT_Doc_Detail_Id.Rows(0).Item("totalpriceprice").ToString
            TxtVat.Value = DT_Doc_Detail_Id.Rows(0).Item("vat").ToString
            TxtWarantry.Value = DT_Doc_Detail_Id.Rows(0).Item("warantry").ToString
            TxtImagepart.Text = DT_Doc_Detail_Id.Rows(0).Item("imagepart").ToString

            If DT_Doc_Detail_Id.Rows(0).Item("nonmfd").ToString = "N" Then
                Me.ChkNonMFD.Checked = False
                DtpMFD.Enabled = True
                DtpMFD.DateTime = DT_Doc_Detail_Id.Rows(0).Item("mdf_date").ToString
            Else
                ChkNonMFD.Checked = True
                DtpMFD.Enabled = False
            End If

            If DT_Doc_Detail_Id.Rows(0).Item("nonbuydate").ToString = "N" Then
                ChkNoneBuyDate.Checked = False
                DtpPHDate.DateTime = DT_Doc_Detail_Id.Rows(0).Item("purchase_date").ToString

            Else
                ChkNoneBuyDate.Checked = True
                DtpPHDate.Enabled = False
            End If

            If DT_Doc_Detail_Id.Rows(0).Item("nonprice").ToString = "N" Then
                ChkNonePrice.Checked = False
                TxtPrice.Text = DT_Doc_Detail_Id.Rows(0).Item("priceperunit").ToString

            Else
                ChkNonePrice.Checked = True
                TxtPrice.Enabled = False
            End If

            If DT_Doc_Detail_Id.Rows(0).Item("nonvat").ToString = "N" Then
                ChkNonVat.Checked = False
                TxtVat.Enabled = True
                TxtVat.Text = DT_Doc_Detail_Id.Rows(0).Item("vat").ToString

            Else
                ChkNonVat.Checked = True
                TxtVat.Enabled = False
                TxtVat.Text = 0
            End If

            '
            CboItemType.SelectedValue = DT_Doc_Detail_Id.Rows(0).Item("item_type_id")


            If Me.TxtImagepart.Text <> "" Then
                If File.Exists(Me.TxtImagepart.Text) = True Then

                    Dim bm As New Bitmap(Me.TxtImagepart.Text)
                    PictureEdit1.Image = bm
                Else
                    MsgBox("ไม่พบรูปนี้จากในระบบ ไฟล์รูปภาพอาจถูกลบ หรือเปลี่ยนที่เก็บ", MsgBoxStyle.Critical, "ไม่สามารถแสดงรูปได้")
                End If

            End If

        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Load_Data"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try
    End Sub
    Sub Update_Data()
        Try
            Strsql = "Update pr_detail set productname= '" & Me.TxtName.Text & "',serialno='" & Me.TxtSerialno.Text & "',band='" & Me.TxtBand.Text & "',model='" & Me.TxtModel.Text & "'"
            Strsql = Strsql & ",priceincludevat =" & Me.TxtPriceIncludeVat.Value & " ,drawqty = " & Me.TxtQty.Value & ",receiveqty = " & Me.TxtReceiveQty.Value & ",totalpriceprice = " & Me.TxtTotalprice.Value & ",staff='" & Me.TxtStaff.Text & "'"
            Strsql = Strsql & ",remarks='" & Me.TxtRemarks.Text & "'"

            'Check nonvat
            If ChkNonePrice.Checked = True Then
                Strsql = Strsql & ",vat = 0,nonvat = 'Y'"
            Else
                Strsql = Strsql & ",vat = " & Me.TxtVat.Value & " ,nonvat = 'N'"
            End If
            'Check nonprice
            If ChkNonePrice.Checked = True Then
                Strsql = Strsql & ",priceperunit = 0,nonprice = 'Y'"
            Else
                Strsql = Strsql & ",priceperunit = " & Me.TxtPrice.Text & ",nonprice = 'N'"
            End If
            'Check nonbuydate 
            If ChkNoneBuyDate.Checked = True Then
                Strsql = Strsql & ",purchase_date = null,nonbuydate = 'Y'"
            Else
                Strsql = Strsql & ",purchase_date = '" & Pn_Framework.MysqlDateTimeFormat(Me.DtpPHDate.DateTime, False) & "',nonbuydate = 'N'"
            End If
            'Check nonmfd
            If ChkNonMFD.Checked = True Then
                Strsql = Strsql & ",mdf_date= null,nonmfd = 'Y'"
            Else
                Strsql = Strsql & ",mdf_date='" & Pn_Framework.MysqlDateTimeFormat(Me.DtpMFD.DateTime, False) & "',nonmfd = 'N'"
            End If

            Strsql = Strsql & ",item_type_id = " & CboItemType.SelectedValue

            Strsql = Strsql & ",warantry =  " & Me.TxtWarantry.Value
            If Me.TxtImagepart.Text = "" Then
                Strsql = Strsql & ",imagepart = ''"
            Else
                Copy_Image2Server()
                Strsql = Strsql & ",imagepart = '" & Replace(Me.TxtImagepart.Text, "\", "\\") & "'"
            End If


            Strsql = Strsql & " where doc_detail_id = " & frmEditPRForm.Primary_Detail_Key
            Select Case _mysql.MySQLExecute(Strsql)
                Case -1
                    With frmDebug
                        .lblFormName.Text = Me.Name
                        .lblFunctionName.Text = "Update_Data"
                        .MemoErr_Description.Text = "กรุณาตรวจสอบคำสั่ง sql"
                        .MemoSQL.Text = Strsql
                        .ShowDialog()
                    End With
                Case 0
                    MsgBox("ไม่พบ Record ที่ต้องการ update", MsgBoxStyle.Exclamation, "ผลการปรับปรุงข้อมูล")

                Case Is > 0
                    MsgBox("ปรับปรุงเรียบร้อยแล้ว", MsgBoxStyle.Information, "ผลการปรับปรุงข้อมูล")
                    Me.DialogResult = Windows.Forms.DialogResult.OK
            End Select

        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Update_Data"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try
    End Sub
    Function validate_data() As Boolean
        Try
            validate_data = False
            If Me.TxtName.Text = "" Then
                MsgBox("กรุณาระบุชื่อสินค้า", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "ไม่สามารถบันทึกข้อมูลได้")

            End If
            validate_data = True
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Update_Data"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try

    End Function
    Sub Update_Price()
        Me.TxtPriceIncludeVat.EditValue = Me.TxtPrice.Value + ((Me.TxtPrice.Value * Me.TxtVat.Value) / 100)
        Me.TxtTotalprice.EditValue = Me.TxtPriceIncludeVat.EditValue * Me.TxtQty.EditValue

    End Sub
    Sub Load_ItemType()
        Try
            Strsql = "Select * from itemtype"
            Dim DT_ItemType As New DataTable
            DT_ItemType = _mysql.GetMYSQLDataTable(Strsql, "itemname")
            With CboItemType

                .DataSource = DT_ItemType
                .DisplayMember = "itemname"
                .ValueMember = "itemnumber"

            End With

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
#End Region

    Private Sub frmEditReceiveItem_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        If Me.DialogResult <> Windows.Forms.DialogResult.OK Then
            If MsgBox("คุณต้องการออกโดยไม่บันทึกการแก้ไขใช่หรือไม่", MsgBoxStyle.Question + MsgBoxStyle.YesNo) = MsgBoxResult.No Then
                Me.DialogResult = Windows.Forms.DialogResult.Cancel
                e.Cancel = True
            End If
        End If

    End Sub
    Private Sub frmEditReceiveItem_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Load_ItemType()
        Load_Data()
        On_load = False
    End Sub

    Private Sub BtnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSave.Click


        If validate_data() = False Then Exit Sub
        Update_Data()

        Me.Dispose()
    End Sub

    Private Sub TxtQty_EditValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TxtQty.EditValueChanged
        Update_Price()
        Me.TxtReceiveQty.Text = Me.TxtQty.Text
    End Sub

    Private Sub TxtTotalprice_EditValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TxtTotalprice.EditValueChanged

    End Sub

    Private Sub TxtPrice_EditValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TxtPrice.EditValueChanged
        Update_Price()
    End Sub

    Private Sub TxtPriceIncludeVat_EditValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TxtPriceIncludeVat.EditValueChanged
        Me.TxtTotalprice.Value = Me.TxtPriceIncludeVat.Value * Me.TxtQty.Value
        Me.TxtReceiveQty.Value = Me.TxtQty.Value
    End Sub

    Private Sub BtnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCancel.Click
        Me.Close()
    End Sub

    Private Sub ChkNonePrice_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ChkNonePrice.CheckedChanged
        If On_load = True Then Exit Sub
        Select Case Me.TxtPrice.Enabled
            Case True
                Me.TxtPrice.Enabled = False
            Case False
                Me.TxtPrice.Enabled = True

        End Select


    End Sub

    Private Sub ChkNonVat_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ChkNonVat.CheckedChanged
        If On_load = True Then Exit Sub
        Select Case Me.TxtVat.Enabled
            Case True
                Me.TxtVat.Enabled = False
                Me.TxtVat.EditValue = 0
            Case False
                Me.TxtVat.Enabled = True
                Me.TxtVat.EditValue = 7

        End Select
    End Sub

    Private Sub ChkNoneBuyDate_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ChkNoneBuyDate.CheckedChanged
        If On_load = True Then Exit Sub
        Select Case Me.DtpPHDate.Enabled
            Case True
                Me.DtpPHDate.Enabled = False
            Case False
                Me.DtpPHDate.Enabled = True

        End Select
    End Sub

    Private Sub ChkNonMFD_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ChkNonMFD.CheckedChanged
        If On_load = True Then Exit Sub
        Select Case Me.DtpMFD.Enabled
            Case True
                Me.DtpMFD.Enabled = False
            Case False
                Me.DtpMFD.Enabled = True

        End Select
    End Sub

    Private Sub TxtVat_EditValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TxtVat.EditValueChanged
        Update_Price()
    End Sub

    Private Sub BtnBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnBrowse.Click
        Try
            With Me.OpenFileDialog1
                .Title = "เลือกรูปภาพ"
                .FileName = ""
                .Filter = "Jpeg Image|*.jpg"
                If .ShowDialog() = Windows.Forms.DialogResult.Cancel Then Exit Sub

                Me.TxtImagepart.Text = .FileName
                Dim bm As New Bitmap(Me.TxtImagepart.Text)
                PictureEdit1.Image = bm

            End With
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "PicLoadImg_Click"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try
    End Sub

    Private Sub CboItemType_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CboItemType.SelectedIndexChanged
        Try
            Strsql = "Select itemimage from itemtype where itemnumber = " & CboItemType.SelectedValue
            Dim MyImgFile As String = _mysql.MySQLExecuteScalar(Strsql)
            If MyImgFile <> "" Then
                Me.TxtImagepart.Text = MyImgFile
                Dim bm As New Bitmap(Me.TxtImagepart.Text)
                PictureEdit1.Image = bm
            End If


        Catch ex As Exception

        End Try
    End Sub
End Class